package com.zhiyou.app.shop.manager.action;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.JsonObject;
import com.zhiyou.app.shop.common.Authority;
import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.InnerObject;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.manager.action.vo.OrderVo;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsOrder;
import com.zhiyou.app.shop.manager.dao.pojo.Shop;
import com.zhiyou.app.shop.manager.service.IOrderService;
import com.zhiyou.app.shop.manager.service.IShopManageService;

public class OrderAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	@Autowired
	private IOrderService orderService;
	@Autowired
	private IShopManageService shopService;
	private GoodsOrder order = new GoodsOrder();
	private OrderVo vo = new OrderVo();
	public OrderAction() {
		super();
		this.setCurrentPath(getText("shopManage.path.myshop.order"));
	}


	/**
	 * 商铺订单管理
	 * @return
	 */
	@Authority(2)
	public String shopOrderList(){
		Page page = getInitedPage();
		try {
			Shop myShop = shopService.findMyShop(this.getCurrentUser().getId());
			if(null != myShop){
				order.setShop(myShop);
				orderService.findMyShopOrder(page, order);
			}
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}

	/**
	 * 将某个订单设置为已发货
	 * @return
	 */
	@Authority(2)
	public String setToDeliverd(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				orderService.setDeliverd(order.getId());
			}
		});
		return null;
	}
	
	/**
	 * 将某个订单设置为已收货
	 * @return
	 */
	@Authority(2)
	public String setToReceived(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				orderService.setReceived(order.getId());
			}
		});
		return null;
	}
	
	/**
	 * 查询的我的购物记录
	 * @return
	 */
	@Authority(2)
	public String myOrderList(){
		this.setCurrentPath(getText("shopManage.path.myorder"));
		Page page = getInitedPage();
		try {
			order.setUserDetail(getCurrentUser());
			orderService.findMyOrders(page, order);
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	/**
	 * 删除未付款订单
	 * @return
	 */
	@Authority(2)
	public String deleteMyOrder(){
		this.printJSON(getResponse(), new InnerObject(){
			public void execute(JsonObject json) {
				orderService.deleteNoPaiedOrder(order.getId());
			}
		});
		return null;
	}
	
	/**
	 * 商城订单统计
	 * @return
	 */
	@Authority(1)
	public String shopOrderReport(){
		this.setCurrentPath(getText("shopManage.path.shopOrder.report"));
		Page page = getInitedPage();
		try {
			orderService.findAllShopOrders(page, vo);
		} catch (Exception e) {
			this.setErrorMessage(e);
		}
		return SUCCESS;
	}
	
	public GoodsOrder getOrder() {
		return order;
	}

	public void setOrder(GoodsOrder order) {
		this.order = order;
	}

	public OrderVo getVo() {
		return vo;
	}

	public void setVo(OrderVo vo) {
		this.vo = vo;
	}
}
